package com.xuecheng.system.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 这里是解决跨域问题
 */
@Component
public class GlobalCorsConfig {
    @Bean
    public CorsFilter corsFilter(){
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //允许所有域名都可以来访问
        corsConfiguration.addAllowedOrigin("*");
        //允许跨域发送Cookie
        corsConfiguration.setAllowCredentials(true);
        //放行所有原始头信息
        corsConfiguration.addAllowedHeader("*");
        //允许所有方法请求调用
        corsConfiguration.addAllowedMethod("*");
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //所有的请求都要被拦截
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
